/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package All.Mapper;

import All.DTO.TintucDTO;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author NGUYEN DANG
 */
public class TintucMapper extends MapperDB
{
    public TintucMapper() throws Exception 
        {
		super();
	}
    public ArrayList<TintucDTO> listTintuc() throws Exception 
    {
        ArrayList<TintucDTO> listOfInfo = new ArrayList<TintucDTO>();
        TintucDTO tintucDTO = null;
        try{
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT * FROM Tintuc");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            while ((rs != null) && rs.next()) 
            {
                    tintucDTO = new TintucDTO();
                    initTintucDTO(tintucDTO, rs);
                    listOfInfo.add(tintucDTO);
            }

            stmt.close();
        }
        catch(Exception e)
        {
            throw e;
        }
        return listOfInfo;
    }

    public void initTintucDTO(TintucDTO tintucDTO, ResultSet rs) throws SQLException
    {
        if ((rs != null) && (tintucDTO != null)) 
        {
            
            tintucDTO.setMatintuc(rs.getString("Mabantin"));
            tintucDTO.setTieude(rs.getString("Tieude"));
            tintucDTO.setTomtat(rs.getString("Tomtat"));
            tintucDTO.setNoidung(rs.getString("Noidung"));
            tintucDTO.setHinhanh(rs.getString("Hinhanh"));
            tintucDTO.setNgaydang(rs.getString("Ngaydang"));
            tintucDTO.setTheloai(rs.getString("Theloai"));
            tintucDTO.setUserid(rs.getString("Nguoidang"));
        }
    }
    public TintucDTO getNewsInformation(int matintuc) throws Exception {
		TintucDTO tintucDTO = new TintucDTO();
		
		try {
			StringBuilder sql = new StringBuilder();
			sql.append("SELECT * FROM Tintuc WHERE ");
			sql.append(" Mabantin = '" + matintuc + "'");
		
			PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
			ResultSet rs = stmt.executeQuery();
			if ((rs != null) && rs.next()) {
				initTintucDTO(tintucDTO, rs);
			}
			
			stmt.close();
		}
		catch (Exception e) {
			throw e;
		}
		
		return tintucDTO;
	}

    public boolean ADUpdateNews(TintucDTO tintucDTO) throws Exception {
        boolean found = false;
        int tmp = Integer.parseInt(tintucDTO.getMatintuc());
        try{
            StringBuilder sql = new StringBuilder();
            sql.append("UPDATE Tintuc set Tieude='" + tintucDTO.getTieude() + "',");
            sql.append(" Tomtat='" + tintucDTO.getTomtat() + "',");
            sql.append(" Noidung='" + tintucDTO.getNoidung() + "',");
            sql.append(" Hinhanh='" + tintucDTO.getHinhanh() + "',");
            sql.append(" Ngaydang='" + tintucDTO.getNgaydang() + "',");            
            sql.append(" Theloai='" + tintucDTO.getTheloai() + "',");
            sql.append(" Nguoidang='" + tintucDTO.getUserid() + "'");
            sql.append(" where Mabantin = '"+ tmp + "'");
/*            sql.append("update Tintuc(Tieude,Tomtat,Noidung,Hinhanh,Ngaydang,Theloai,Nguoidang) values(");
            sql.append("'" + tintucDTO.getTieude() +"',");
            sql.append("'" + tintucDTO.getTomtat() +"',");
            sql.append("'" + tintucDTO.getNoidung() +"',");
            sql.append("'" + tintucDTO.getHinhanh() +"',");
            sql.append("'" + tintucDTO.getNgaydang() +"',");
            sql.append("'" + tintucDTO.getTheloai() +"',");
            sql.append("'" + tintucDTO.getUserid() +"')");
            sql.append(" where Mabantin="+tmp);
 */           
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            int flag = stmt.executeUpdate();
            if(flag == 1)
                    found = true;
            stmt.close();
        }
        catch(Exception e)
        {
            throw e;
        }
        return found;
    }

    public boolean InsertNews(TintucDTO tintucDTO) throws Exception {
        boolean found = false;
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("insert into Tintuc(Tieude,Tomtat,Noidung,Hinhanh,Ngaydang,Theloai,Nguoidang) values(");
            sql.append("'" + tintucDTO.getTieude() +"',");
            sql.append("'" + tintucDTO.getTomtat() +"',");
            sql.append("'" + tintucDTO.getNoidung() +"',");
            sql.append("'" + tintucDTO.getHinhanh() +"',");
            sql.append("'" + tintucDTO.getNgaydang() +"',");
            sql.append("'" + tintucDTO.getTheloai() +"',");
            sql.append("'" + tintucDTO.getUserid() +"')");
            
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());

            int flag = stmt.executeUpdate();
            if(flag == 1)
                    found = true;
            stmt.close();
        }
        catch(Exception e)
        {
            throw e;
        }
        return found;
    }

    public boolean ADDeleteNews(String matin) throws Exception 
    {
        boolean found = false;
        try{
            StringBuilder sql = new StringBuilder();
            sql.append("Delete from Tintuc where Mabantin=");
            sql.append("'"+matin+"'");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            int flag = stmt.executeUpdate();
            if(flag == 1)
                    found = true;
            stmt.close();
        }
        catch(Exception e)
        {
            throw e;
        }
        return found;
    }
}
